<?php
/**
 * Company Detail Screen
 * 
 * All company master data are displayed in this screen and 
 * can be changed
 * 
 * @package Hive\Company
 * @author Xavier VILAIN
 * @license
 */
use Fox\Html\Html;
use Fox\Html\Anchor;
use Fox\Generator;
use Fox\Utilities;
use Fox\Html\Header\Content;
use Fox\Html\Screen;
use Hive\Company;

$xview = Xview::instance();
/* @var $company Hive\Company */
$company = Xview::instance()->component(Company::BUNDLE, 'object');
$adressLink = $company->defaultAddress();
$id = $company->getId();
$data = $company->getData();
if(!empty($adressLink)) {
	$data = array_merge($data,$adressLink->address()->getData());
}
// Customizing
$customizing = Hive\Company\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'company','act'=>'detail','sac'=>null));

$data['company_parent'] = $company->parentName();

$title = new Content('detail',Msg(Hive::BUNDLE,Hive::COMPANY_MENU_DETAIL));
$screen = new Screen('sCompany','update');
$screen->action(Generator::url('company','detail',null,array('xvtrs' => 'none','cpid' => $id)));
$screen->ajaxUpload(Generator::redirect(Generator::url('company','main',null,array('cpid' => $id))),'contentArea');

// Master Data
$blInfo = $xview->component(Company::BUNDLE, 'header');
if(!empty($data['company_parent'])) {
	$linkGen = Generator::url('company','main',null,array('cpid' => $data['parentid']));
	$link =  Generator::generateValue($linkGen,array());
	$blInfo->item('company_parent')->add(new Html('linkToParent','<a style="font-size: 11px;margin-left: 5px;" class="slink" href="'.$link.'">'.$data['company_parent'].'</a>'));
}
$screen->add($blInfo);
// Address Data
$blAddress = $xview->component(Company::BUNDLE, 'address');
$screen->add($blAddress);
$screen->initialize($data);

if($screen->submitted())
{
	$fields = $screen->get();

	if(!$screen->errors())
	{
		try {
			$file = Xview::moveFileToServer(null,_APPLICATION_IMAGES_COMPANIES_PATH_,$company->getId());
			if(!empty($file['physicalname'])) {
				$fields['logo'] = $file['physicalname'];
				$realImage = new \Fox\Core\Image(_APPLICATION_IMAGES_COMPANIES_PATH_.$file['physicalname']);
				$fileNameMini = 'thumb_'.$file['physicalname'];
				$realImage->resize(40,40);
				$realImage->export(_APPLICATION_IMAGES_COMPANIES_PATH_.$fileNameMini);
			}
		}
		catch (Exception $e) {
			//Fox\Message::Error($e);
		}
		try {

			// Handle Parent
			if(!empty($fields['company_parent']))
			{
				$companyParent = Company::lookup($fields['company_parent']);
				if(!$companyParent)
				{
					$companyParent = new Company();
					$companyParent->setFields(array("company_name" => $fields['company_parent']));
					$companyParent->create();
				}
				$fields['parentid'] = $companyParent->getId();
			}
			if(empty($adressLink)) {
				$address = new Fox\Common\Address();
				$address->setFields($fields);
				$address->create();
				$adressLink = new \Hive\Company\Address\Link();
				$adressLink->setFields(array(
					'compid' => $company->getId(),
					'adrid' => $address->getId(),
					'type' => Company::ADDRESS_TYPE_DEFAULT
				));
				$company->addToAddresses($adressLink);
			} else {
				$address = $adressLink->address();
				$address->setFields($fields);
				$address->update();
			}
			// Handle Address
			$company->setFields($fields);
		
			// Update
		
			$company->prepareUpdate(Company::CHANGE_DEFAULT);
			if($company->update()) {
				Fox\Message::Success(Msg(Hive::BUNDLE,
										Hive::COMPANY_SUCCESSFULLY_UPDATED,
										$fields['company_name']));
			} else {
				Fox\Message::Information(Msg(Fox\Msg::BUNDLE,
										Fox\Msg::MSG_NO_UPDATE,
										$fields['company_name']));
			}
		}
		catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
	
	if(!$screen->errors()) {
		return $xview->messages(true,true);
	}
}
?>
<div id="contentArea">
	<div><?php echo $title->render();?></div>
	<div class="contentArea" >
		<div class="mbs gsl" id="contacts-container">
				<?php echo $screen->display(); ?>
		</div>
	</div>
</div>